package com.jyy.prefabricated.service.impl;

import com.jyy.prefabricated.dto.ChannelDto;
import com.jyy.prefabricated.entity.Channel;
import com.jyy.prefabricated.entity.OrderInfo;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ChannelMapper;
import com.jyy.prefabricated.mapper.OrderInfoMapper;
import com.jyy.prefabricated.mapper.SysUserMapper;
import com.jyy.prefabricated.service.ChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 渠道表(Channel)表服务实现类
 *
 * @author mrl
 * @since 2021-07-17 14:51:53
 */
@Service("channelService")
public class ChannelServiceImpl implements ChannelService {
    @Resource
    private ChannelMapper channelDao;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param channelId 主键
     * @return 实例对象
     */
    @Override
    public Channel queryById(Integer channelId) {
        return this.channelDao.queryById(channelId);
    }

    @Override
    public ChannelDto selectDetail(Integer channelId) {
        if(channelId == null){
            throw new CustomException("参数id不能为空");
        }
        ChannelDto result = new ChannelDto();
        Channel channel = channelDao.queryById(channelId);
        result.setChannelName(channel.getChannelName());
        result.setChannelCode(channel.getChannelCode());
        result.setChannelId(channel.getChannelId());
        if(result.getChannelCode() != null && !"".equals(result.getChannelCode())){
            List<SysUser> sysUsers = userMapper.selectByChannel(result.getChannelCode());
            if(sysUsers != null){
                result.setCustomerNum(sysUsers.size());
                result.setCustomerList(sysUsers);
                List<OrderInfo> orderInfos = orderInfoMapper.selectOrderList(result.getChannelCode());
                if(orderInfos != null){
                    result.setOrderNum(orderInfos.size());
                    result.setOrderList(orderInfos);
                }else{
                    result.setOrderNum(0);
                }
            }else{
                result.setCustomerNum(0);
                result.setOrderNum(0);
            }

        }
        return result;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Channel> queryAllByLimit(int offset, int limit) {
        return this.channelDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<ChannelDto> selectByParams(ChannelDto channelDto) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelDto,channel);
        List<Channel> channels = channelDao.queryAll(channel);
        List<ChannelDto> result = new ArrayList<>();
        for (Channel channelItem:channels) {
            ChannelDto channelResult = new ChannelDto();
            BeanUtils.copyProperties(channelItem,channelResult);

            Integer orderNum = userMapper.selectByChannelCode(channelItem.getChannelCode());
            if(orderNum != null && orderNum != 0){
                channelResult.setCustomerNum(orderNum);
                Integer courseOrder = channelDao.selectCourseOrder(channelItem.getChannelCode());
                /*int traProjectOrder = channelDao.selectProjectOrderByTra(channelItem.getChannelCode());
                int stuCourseOrder = channelDao.selectProjectOrderByStudent(channelItem.getChannelCode());
                channelResult.setOrderNum(courseOrder+traProjectOrder+stuCourseOrder);*/
                if(courseOrder != null && courseOrder != 0){
                    channelResult.setOrderNum(courseOrder);
                }else{
                    channelResult.setOrderNum(0);
                }
            } else{
                channelResult.setOrderNum(0);
                channelResult.setCustomerNum(0);
            }
            result.add(channelResult);

        }

        return result;
    }

    /**
     * 新增数据
     *
     * @param channelDto 实例对象
     * @return 实例对象
     */
    @Override
    public Channel insert(ChannelDto channelDto) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(channelDto,channel);
        channel.setCreateTime(new Date());
        channel.setUpdateTime(new Date());
        this.channelDao.insert(channel);
        return channel;
    }

    /**
     * 修改数据
     *
     * @param channel 实例对象
     * @return 实例对象
     */
    @Override
    public Channel update(Channel channel) {
        channel.setUpdateTime(new Date());
        Channel oldChannel = this.queryById(channel.getChannelId());
        String oldCode = oldChannel.getChannelCode();
        int count = this.channelDao.update(channel);
        if(count > 0 && channel.getChannelCode() != null && oldCode != null && !channel.getChannelCode().equals(oldCode)){
            userMapper.updateByCode(oldCode,channel.getChannelCode());
        }
        return this.queryById(channel.getChannelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param channelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer channelId) {
        return this.channelDao.deleteById(channelId) > 0;
    }

    @Override
    public int batchDelete(List<Integer> list) {
        if(list.size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return channelDao.batchDelete(list);
    }
}